<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <%- include ../shop/common.ejs%>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-xs-12" style="padding: 10px 0 10px 15px">
            <a href="/goods/add"><button type="button" class="btn btn-default">新增商品</button></a>&nbsp;&nbsp;
      <!--/goods/edit-->      <a href="#" onclick="getCheckIds()"><button type="button" class="btn btn-default">编辑商品</button></a>&nbsp;&nbsp;
            <button type="button" class="btn btn-default">下架商品</button>&nbsp;&nbsp;
            <a href="#" onclick="del()"><button type="button" class="btn btn-default">删除商品</button></a>
        </div>

        <div class="col-md-12">
            <table class="table table-striped" id="goodsTable">
                <thead>
                <tr>
                    <th>√</th>
                    <th>#</th>
                    <th>商品名称</th>
                    <th>商品描述</th>
                    <th>商品编号</th>
                    <th>价格</th>
                    <th>参与积分</th>
                    <th>状态</th>
                    <th>创建时间</th>
                </tr>
                </thead>
                <tbody>
                <%data.rows.forEach(function(goods,index){%>
                <tr>
                    <th><input name="goods_id" type="checkbox" value="<%=goods.id%>"></th>
                    <th scope="row"><%=index+1%></th>
                    <td><%=goods.title%></td>
                    <td><%=goods.description%></td>
                    <td><%=goods.goodssn%></td>
                    <td><%=goods.productprice%></td>
                    <td><%=goods.credit%></td>
                    <td><%=goods.status%></td>
                    <td><%=goods.createtime%></td>

                </tr>
                <%})%>


                </tbody>
            </table>
        </div>
        <div class="col-xs-12">
            <ul id="display-page"></ul>
        </div>
    </div>
</div>
<!--
&lt;!&ndash; 模态框（Modal） &ndash;&gt;
<div class="modal fade" id="myModal" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close"
                        data-dismiss="modal" aria-hidden="true">
                    &times;
                </button>
                <h4 class="modal-title" id="myModalLabel">
                    设置中奖率
                </h4>
            </div>
            <div class="modal-body"  style="width: 700px;">

            </div>
            <div class="modal-footer" style="padding-bottom: 15px;">
                &lt;!&ndash;<button type="button" class="btn btn-default" style="height: 30px;" data-dismiss="modal">关闭
                </button>
                <button type="button" class="btn btn-primary" style="margin-bottom: 15px !important;height: 30px;">
                  提交更改
                </button>&ndash;&gt;
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                </button>
            </div>
        </div>&lt;!&ndash; /.modal-content &ndash;&gt;
    </div>&lt;!&ndash; /.modal &ndash;&gt;
</div>

<button class="btn btn-primary"  id="openModal">
    上传图片
</button>-->
</body>
<script>
/*
    $('#openModal').click(function(){
        $('#myModal').modal('toggle');
    });*/
    init(<%= data.total%>);//初始化分页
    function init(total){
        if(total==0){
            total=1;
        }
        var options = {
            currentPage: 1,
            totalPages: parseInt((Number(total)+3-1)/3,10),
            alignment:'center',
            bootstrapMajorVersion:3,
            tooltipTitles: function (type, page, current) {
                switch (type) {
                    case "first":
                        return "跳转到首页";
                    case "prev":
                        return "上一页";
                    case "next":
                        return "下一页";
                    case "last":
                        return "跳转到末页";
                    case "page":
                        return "转到 " + page;
                }
            },
            onPageClicked:function(event, originalEvent, type,page){
                reloadpage(page,false);
            }
        }

        $('#display-page').bootstrapPaginator(options);
    }
    function reloadpage(page,isinit){
        $.get("/goods/list",
                {page:page},
                function(result){
                    if(isinit){
                        init(result.total);
                    }

                    if(result.rows.length<1){
                        $('#rowcontent').html('无数据...');
                        return;
                    }
                    var html = '';
                    result.rows.forEach(function(goods,index){
                        html+='<tr><th><input name="goods_id" type="checkbox" value="'+goods.id+'"></th> <th scope="row">'+(index+1)+'</th><td>'+goods.title+'</td>' +
                                '<td>'+goods.description+'</td> <td>'+goods.goodssn+'</td> <td>'
                                +goods.productprice+'</td> <td>'+goods.credit+'</td> <td>'
                                +goods.status+'</td> <td>'+goods.createtime+'</td></tr>';
                    });
                    $('#goodsTable tbody').html(html);
                });
    }


    function getCheckIds(){
        var adIds = "";
        $("input:checkbox[name='goods_id']:checked").each(function(i){
            if(0==i){
                adIds = $(this).val();
            }
        });
        if(!adIds){
            return alert('请选择一件商品!');
        }
        window.location.href="/goods/edit?id="+adIds;
    }
    function getCheckIds1(){
        var adIds = "";
        $("input:checkbox[name='goods_id']:checked").each(function(i){
        if(0==i){
            adIds = $(this).val();
        }else{
            adIds += (","+$(this).val());
        }
//            alert(adIds);
    });

    }
function del(){
    var id = selectid();
    if(!id){
        return alert('请选择一条记录!');
    }
    $.post('/goods/del',{id:id},function(result){
        if(result.msg){
            location.reload(true);
        }else{
            alert('失败！');
        }
    });
}

function selectid(){
    var adIds = "";
    $("input:checkbox[name='goods_id']:checked").each(function(i){
        if(0==i){
            adIds = $(this).val();

        }
    });
    if(!adIds){
        return alert('请选择一条记录!');
    }
    return adIds;
}
</script>
</html>